from pytest import raises
from pathlib import Path


def test_class_init():
    from hz_utils.jsonlines import JsonLReader
    test_dir = Path(__file__).parent
    reader = JsonLReader(test_dir / "./case1.jsonl")
    assert len(reader.lines) == 7


def test_find_one():
    from hz_utils.jsonlines import JsonLReader
    test_dir = Path(__file__).parent
    reader = JsonLReader(test_dir / "./case1.jsonl")
    assert reader.find_one("name = 'Leon Bruce'")
    assert len(reader.find_all("name = 'Leon Bruce'")) == 5
    assert len(reader.find_all("name = Jack")) == 2
    assert len(reader.find_all("name = 'Jack'")) == 2
    assert len(reader.find_all("age > 10")) == 4
    assert len(reader.find_all("age > 10 or age = 10")) == 5
    assert len(reader.find_all("title is null")) == 6
    assert len(reader.find_all("title is not null")) == 1
    assert len(reader.find_all("age >= 10")) == 5
    assert len(reader.find_all("age >= 11")) == 4
    assert len(reader.find_all("age <= 11")) == 4
    assert len(reader.find_all("age < 11")) == 3

def test_and_or():
    from hz_utils.jsonlines import JsonLReader
    test_dir = Path(__file__).parent
    reader = JsonLReader(test_dir / "./case1.jsonl")
    assert len(reader.find_all("name = Jack and age >12")) == 1
    assert len(reader.find_all("name = Jack and age =12")) == 1

def test_regex():
    from hz_utils.jsonlines import JsonLReader
    test_dir = Path(__file__).parent
    reader = JsonLReader(test_dir / "./case1.jsonl")
    assert len(reader.find_all("name like 'J%'")) == 2
